﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Garlic.BLL;
using Garlic.DM.DataModel;
using System.Collections.Generic;
using Garlic.CC.BaseClass;

public partial class SystemManagement_MenuTree : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                string isMultiSelect = Request.QueryString["multiSelect"];

                if (string.IsNullOrEmpty(isMultiSelect) || isMultiSelect == "false")
                {
                    //ClientScript.RegisterClientScriptBlock(this.GetType, "", "");
                    mnuTree.Attributes.Add("onclick", "SelectOneCheckbox()");
                }
                else
                {
                    mnuTree.Attributes.Add("onclick", "CheckEvent()");
                }

                TreeNode rootNode = new TreeNode("系统", "0");
                rootNode.ShowCheckBox = false;

                List<MenuInfo> menus = MenuManager.GetTopMenus();

                foreach (MenuInfo menu in menus)
                {
                    TreeNode node = new TreeNode(menu.Menu_name, menu.Menu_id.ToString());
                    GetSubMenu(node.ChildNodes, menu.Menu_id);

                    node.NavigateUrl = "javascript:void(0)";

                    rootNode.ChildNodes.Add(node);
                }

                mnuTree.Nodes.Add(rootNode);
            }
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }

    private void GetSubMenu(TreeNodeCollection treeNodeCollection, int parent_menu_id)
    {
        List<MenuInfo> menus = MenuManager.GetSubMenus(parent_menu_id);

        foreach (MenuInfo menu in menus)
        {
            TreeNode node = new TreeNode(menu.Menu_name, menu.Menu_id.ToString());
            GetSubMenu(node.ChildNodes, menu.Menu_id);

            node.NavigateUrl = "javascript:void(0)";

            treeNodeCollection.Add(node);
        }
    }


    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            string results = "";
            if (mnuTree.CheckedNodes.Count > 0)
            {
                foreach (TreeNode node in mnuTree.CheckedNodes)
                {
                    results += node.Text + "," + node.Value + ";";
                }

                results = results.Substring(0, results.Length - 1);
            }
            else if (mnuTree.SelectedNode != null)
            {
                results = mnuTree.SelectedNode.Text + "," + mnuTree.SelectedNode.Value;
            }

            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script type='text/javascript'>window.parent.returnValue = '" + results + "';window.parent.close()</script>");
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }
}
